<?php

namespace app\admin\model\store;

use app\admin\model\shop\Paidan;
use app\admin\model\shop\user\User;
use think\facade\Db;
use think\Model;

/**
 * Order
 */
class Order extends Model
{
    // 表主键
    protected $pk = 'order_id';

    // 表名
    protected $name = 'store_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 字段类型转换
    protected $type = [
        'ship_done_time' => 'timestamp:Y-m-d H:i:s',
    ];


    public function getProductPriceAttr($value): float
    {
        return (float)$value;
    }

    public function getFreightAttr($value): float
    {
        return (float)$value;
    }

    public function getDiscountedPriceAttr($value): float
    {
        return (float)$value;
    }

    public function getCommissionFeeAttr($value): float
    {
        return (float)$value;
    }

    public function getPredictCommissionFeeAttr($value): float
    {
        return (float)$value;
    }

    public function store(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(Store::class, 'store_id', 'id');
    }

    public function shopUser(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function endOrder($order_id)
    {
        $result['status']  = false;
        $result['msg']  = '';
        $user_id = self::where('order_id', $order_id)->value('user_id');
        if(!$user_id){
            return false;
        }
        $userinfo = User::where('id', $user_id)->find();
        if(!$userinfo){
            return false;
        }
        try {
            //主店id
            $store_id = Db::name('store_store')->where('radio', 1)->value('id');
            //计算合伙人等级
            if($userinfo['partner_id'] != 3){
                if(isset($store_id)){
                    $order_price = self::where('store_id', $store_id)->where('order_status',100)->sum('order_price');
                    $config_money = Db::name('recommend_partner')->where('id',3)->value('money');
                    $config_money2 = Db::name('recommend_partner')->where('id',2)->value('money');
                    if(isset($config_money) && $order_price && $config_money <= $order_price){
                        User::where('id', $user_id)->update(['partner_id' => 3]);
                    }elseif (isset($config_money2) && $order_price && $config_money2 <= $order_price){
                        User::where('id', $user_id)->update(['partner_id' => 2]);
                    }else{
                        $config_money = Db::name('recommend_partner')->where('id',1)->value('money');
                        $order_price = self::where('order_status',100)->sum('order_price');
                        if(isset($config_money) && $order_price && $config_money <= $order_price){
                            User::where('id', $user_id)->update(['partner_id' => 1]);
                        }
                    }
                }else{
                    $config_money = Db::name('recommend_partner')->where('id',1)->value('money');
                    $order_price = self::where('order_status',100)->sum('order_price');
                    if(isset($config_money) && $order_price && $config_money <= $order_price){
                        User::where('id', $user_id)->update(['partner_id' => 1]);
                    }
                }
            }
            //减用户可派单数量
            if($userinfo['pstatus'] == 1){
                $model = new Paidan();
                $model->change($user_id,$model::TYPE_SUCCESS,1,$order_id);
                User::where('id', $user_id)->setInc('ypnum', 1);
                //禁止派单状态
                $termination = Db::name('recommend_setting')->where('name','termination')->value('value');
                if($termination <= (User::where('id', $user_id)->value('ypnum'))){
                    User::where('id', $user_id)->update(['end_order_id' => self::where('order_id',$order_id)->value('id'),'pstatus'=>0]);
                }
            }else{
                //恢复派单状态
                $restore = Db::name('recommend_setting')->where('name','restore')->value('value');
                if(isset($restore)){
                    if(isset($userinfo['end_order_id'])){
                        $count = self::where('store_id', $store_id)->where('id','>',$userinfo['end_order_id'])->where('order_status',100)->count();
                    }else{
                        $count = self::where('store_id', $store_id)->where('order_status',100)->count();
                    }
                    if($count >= $restore){
                        User::where('id', $user_id)->update(['end_order_id' => '','pstatus'=>1, 'ypnum'=>0]);
                    }
                }
            }
            $result['status']  = true;
            $result['msg']  = 'success';
            $this->commit();

        }catch (\Exception $e){
            $this->rollback();
            $result['status']  = false;
            $result['msg']  = $e->getMessage();
        }
        return $result;
    }
}